
var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
var mph = require("../../utils/util.js");

var app = getApp();
Page({
  data: {
    aaaa:'',
    bbbb:'',
    voucher_id:'',
    my_uid_1: false,
    my_uid_2: false,
    my_uid_3: false,
    my_uid_4: false,
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    discount_code:'',
    get_gift_voucher_list:[],
    my_goods_voucher_list:[],
    voucher_list:[],
    expertList: [
      { //假数据
        id:1,
        u_name:'quanbu',
        text:'优惠券',
        data_list:[]
      },
      { //假数据
        id: 2,
        u_name: 'daizhifu',
        text: '商品券',
        data_list: []
      },
      { //假数据
        id: 3,
        u_name: 'daipeisong',
        text: '商品礼券',
        data_list:[]
      },
      { //假数据
        id: 4,
        u_name: 'daipeisong',
        text: '卡',
        data_list: []
      }
    ],
    formNameV:'',
    showgift_voucher:false,
    sn:'',
    share_bimg:{}
  },
  go_product_detail(e){
    console.log(e);
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/product_detail/product_detail?goods_id='+id
    })
  },
  // go_order_detail(){
  //   wx.navigateTo({
  //     url: '/pages/order_detail/order_detail'
  //   })
  // },
  // go_shopping_cart(){//跳转购物车
  //   wx.switchTab({
  //     url: '/pages/shopping_cart/shopping_cart'
  //   })
  // },
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur,
      })
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  //获取优惠券
  //index.php?act=member_voucher&op=voucher_list
  voucher_list(){
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=member_voucher&op=voucher_list',
      data: {
        key: k,
        voucher_state: 1   //代金券状态(1-未使用 2-已使用 3-已过期)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        if (result.statusCode == 200) {
          let res = result.data.datas.voucher_list;
          if (result.data.code == 200) {
            let arr = [];
            for (let i = 0; i < res.length; i++) {
              let obj = {
                c_id: res[i].voucher_id,
                time: res[i].voucher_end_date_text,
                state: res[i].voucher_state_text,
                img_url: res[i].store_avatar_url,
                text_name: res[i].voucher_title,
                x_num: '',
                res: res[i].voucher_limit
              }
              arr.push(obj);
            }

            let datalist = "expertList[0].data_list";

            that.setData({
              [datalist]: arr
            })

          } else {
            console.log('失败的时候获取获取优惠券:==>', res);

          }
        }
      }
    });
  },
  //-分享优惠券给别人
  //index.php?act=other_member&op=send_vou
  send_vouvoucher_list(id){
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=other_member&op=send_vou',
      data: {
        key: k,
        vou_id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('分享优惠券给别人:==>', result);
        if (result.statusCode == 200) {
          let res = result.data.datas;
          if (result.data.code == 200) {
            console.log('分享优惠券给别人:==>', res);
          } else {
            console.log('分享优惠券给别人:==>', res);

          }
        }
      }
    });
  },
  //领取分享的优惠券
  //index.php?act=other_member&op=get_vou
  get_vouvoucher_list() {
    let that = this;
    let k = wx.getStorageSync('key');
    let voucher_id = wx.getStorageSync('voucher_id'); 
    wx.request({
      url: ApiUrl + '/index.php?act=other_member&op=get_vou',
      data: {
        act:'other_member',
        op:'get_my_vou',
        key: k,
        vou_id: voucher_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        if (result.statusCode == 200) {
          let res = result.data.datas;
          if (result.data.code == 200) {
            that.setData({
              my_uid_1: false
            })
            wx.showToast({
              title: '领取成功',
              icon: 'success',
              duration: 1000
            })
          } else {
            that.setData({
              aaa: res
            })

          }
        }
      }
    });
  },
  //<<<<<<<<<<<<<<<获取商品券>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  //index.php?act=other_member&op=my_goods_voucher
  my_goods_voucher(){
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=other_member&op=my_goods_voucher',
      data: {
        key: k
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('获取商品券:==>', result);
        if (result.statusCode == 200) {
          let res = result.data.datas;

          if (result.data.code == 200) {
            console.log('获取商品券:==>', res, result.data.datas);

            let arr = [];
            for (let i = 0; i < res.length;i++){
              let obj = {
                c_id: res[i].id,
                time: '2019-12-17 16:40:20',
                state: '即将过期',
                img_url: res[i].goods_image,
                text_name: res[i].goods_name,
                x_num: '',
                res: res[i].goods_price,
                goods_id: res[i].goods_id
              } 
              arr.push(obj);
            }

            let datalist = "expertList[1].data_list";

            that.setData({
              [datalist]: arr
            })

          } else {
            console.log('失败的时候获取商品券:==>', res);

          }
        }
      }
    });
  },
  //商品券-分享券给别人
  //index.php?act=other_member&op=send_goods_vou
  send_goods_vou(id){
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=other_member&op=send_goods_vou',
      data: {
        key: k,
        vou_id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('分享商品券给别人:==>', result);
        if (result.statusCode == 200) {
          let res = result.data.datas;
          if (result.data.code == 200) {
            console.log('分享商品券给别人:==>', res);
          } else {
            console.log('分享商品券给别人:==>', res);

          }
        }
      }
    });
  },
  //商品券-领取别人分享的商品券
  //index.php?act=other_member&op=get_my_vou
  get_my_vou(){
    let that = this;
    let k = wx.getStorageSync('key');
    let voucher_id = wx.getStorageSync('voucher_id');
    wx.request({
      url: ApiUrl + '/index.php?act=other_member&op=get_my_vou',
      data: {
        act: 'other_member',
        op: 'get_my_vou',
        key: k,
        vou_id: voucher_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('领取分享的商品券:==>', result);
        if (result.statusCode == 200) {
          let res = result.data.datas;
          if (result.data.code == 200) {
            that.setData({
              my_uid_2: false
            })

            wx.showToast({
              title: '领取成功',
              icon: 'success',
              duration: 1000
            })
            console.log('领取分享的商品券:==>', res, k, fid);
          } else {
            console.log('领取分享的商品券:==>', res);
            that.setData({
              aaa: res
            })

          }
        }
      }
    });
  },

  //商品礼券列表>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  //index.php?act=gift_voucher&op=gift_list
  //获取商品礼券列表
  get_gift_voucher(){
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=gift_voucher&op=gift_list',
      data: {
        act:'gift_voucher',
        op:'gift_list',
        key: k
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('获取商品礼券:==>', result);
        if (result.statusCode == 200) {
          let res = result.data.datas.list;
          if (result.data.code == 200) {
            console.log('获取商品礼券:==>', res);

            let arr = [];
            for (let i = 0; i < res.length; i++) {
              let obj = {
                c_id: res[i].id,
                time: res[i].update_time,
                state: '即将过期',
                img_url: res[i].goods_image,
                text_name: res[i].goods_name,
                x_num: res[i].sn,
                res: res[i].ratio,
                sn: res[i].sn,
                goods_id: res[i].goods_id
              }
              arr.push(obj);
            }

            let datalist = "expertList[2].data_list";

            that.setData({
              [datalist]: arr
            })
            // that.setData({
            //   get_gift_voucher_list: res
            // })
          } else {
            console.log('失败的时候获取商品礼券:==>', res);

          }
        }
      }
    });
  },
  //商品礼券-分享礼券
  //index.php?act=gift_voucher&op=send_gift
  send_gift(id){
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=gift_voucher&op=send_gift',
      data: {
        key: k,
        vou_id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('分享商品礼券给别人:==>', result);
        if (result.statusCode == 200) {
          let res = result.data.datas;
          if (result.data.code == 200) {
            console.log('分享商品礼券给别人:==>', res);
          } else {
            console.log('分享商品礼券给别人:==>', res);

          }
        }
      }
    });
  },
  //商品礼券-领取礼券
  //index.php?act=gift_voucher&op=get_my_gift
  get_my_gift(){
    let that = this;
    let k = wx.getStorageSync('key');
    let voucher_id = wx.getStorageSync('voucher_id');
    wx.request({
      url: ApiUrl + '/index.php?act=gift_voucher&op=get_my_gift',
      data: {
        act: 'other_member',
        op: 'get_my_vou',
        key: k,
        vou_id: voucher_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('领取分享的商品礼券:==>', result);
        if (result.statusCode == 200) {
          let res = result.data.datas;
          if (result.data.code == 200) {
            that.setData({
              my_uid_3: false
            })

            wx.showToast({
              title: '领取成功',
              icon: 'success',
              duration: 1000
            })
            console.log('领取分享的商品礼券:==>', res, k, fid);
          } else {
            console.log('领取分享的商品礼券:==>', res);
            that.setData({
              aaa: res
            })

          }
        }
      }
    });
  },
  //index.php?act=gift_voucher&op=explode_gift
  gift_voucher_btn(){
    let that = this;
    let k = wx.getStorageSync('key');
    let sn = that.data.sn;
    let formNameV = that.data.formNameV;
    console.log(sn, formNameV);
    wx.request({
      url: ApiUrl + '/index.php?act=gift_voucher&op=explode_gift',
      data: {
        act: 'gift_voucher',
        op: 'explode_gift',
        vou_sn: sn,
        num: formNameV,
        key: k
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('获取商品礼券:==>', result);
        if (result.statusCode == 200) {
          let res = result.data.datas;
          if (result.data.code == 200) {
            console.log('获取商品礼券:==>', res);
            that.setData({
              showgift_voucher: false
            })
            that.get_gift_voucher();
          } else {
            console.log('失败的时候获取商品礼券:==>', res);
            
          }
        }
      }
    });
  },
  formName: function (e) {
    this.setData({
      formNameV: e.detail.value
    })
  },
  showgift_voucher(e){
    let sn = e.currentTarget.dataset.sn;
    this.setData({
      showgift_voucher: true,
      sn:sn
    })
  },
  hidebox(){
    this.setData({
      showgift_voucher: false,
      sn: sn
    })
  },
  aaa(){
    //阻止冒泡
  },
  //临时会员卡
  get_vip_flag_list() {
    let that = this;
    var url = 'index.php?act=vipopen&op=vip_flag_list';
    let k = wx.getStorageSync('key');

    let list = { act: 'vipopen', op: 'vip_flag_list', key: k };
    mph.postRequest(url, list, function (res) {
      // res.data.datas.flag_list/
      console.log('临时会员卡:==?:',res.data.datas,res);
      if (res.data.code == 200) {
        console.log(res.data.datas);
        let reslist = res.data.datas.info;
          let obj = {
            c_id: reslist.id,
            time: '2019-12-17 16:40:20',
            state: '即将过期',
            img_url: '/image/1576832596340.jpg',
            text_name: reslist.name,
            x_num: 4,
            res: 35.6
          }
        let datalist = "expertList[3].data_list";
        that.setData({
          [datalist]: res.data.datas.flag_list,
          flag_info: obj
        })
      }
    })
  },
  //会员-我的临时会员卡-激活
  //index.php?act=vipopen&op=use_temp_flag
  use_temp_flag_btn(){
    let that = this;
    let k = wx.getStorageSync('key');
    let voucher_id = wx.getStorageSync('voucher_id');
    wx.request({
      url: ApiUrl + '/index.php?act=vipopen&op=use_temp_flag',
      data: {
        key: k,
        flag: voucher_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        if (result.statusCode == 200) {

          let res = result.data.datas;
          if (result.data.code == 200) {
            that.setData({
              my_uid_4: false
            })

            wx.showToast({
              title: res,
              icon: 'success',
              duration: 1000
            })
          } else {
            that.setData({
              my_uid_4: false
            })

            wx.showToast({
              title: res.error,
              icon: 'success',
              duration: 1000
            })
          }
        }
      }
    });
  },
  //优惠码-填入优惠码，兑换
  //index.php?act=other_member&op=get_discount_code
  bindKeyInput(e){
    this.setData({
      discount_code: e.detail.value
    })
  },
  get_discount_code(){
    let that = this;
    let k = wx.getStorageSync('key');
    let code = that.data.discount_code;
    console.log('填入优惠码，兑换====>',code);
    wx.request({
      url: ApiUrl + '/index.php?act=other_member&op=get_discount_code',
      data: {
        key: k,
        sn: code
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('优惠码-填入优惠码，兑换:==>', result);
        if (result.statusCode == 200) {
          let res = result.data.datas;
          if (result.data.code == 200) {
            console.log('优惠码-填入优惠码，兑换:==>', res);
            wx.showToast({
              title: '兑换成功',
              icon: 'success',
              duration: 2000
            })
          } else {
            console.log('优惠码-填入优惠码，兑换:==>', res);

            wx.showToast({
              title: '兑换码不存在',
              icon: 'none',
              duration: 2000
            })
          }
        }
      }
    });
  },
  onLoad: function (option) {
    let that = this;
    that.my_goods_voucher();
    that.get_gift_voucher();
    that.voucher_list();
    that.get_vip_flag_list();
    that.share_bimg();
    if (option.voucher_id){
      that.setData({
        aaa: option.voucher_id,
        bbb: option.id
      })
      wx.setStorageSync('voucher_id', option.voucher_id); 

      if (option.id == 1) {
        that.setData({
          my_uid_1: true,
          currentTab: 0
        })

      }else if(option.id == 2) {
        that.setData({
          my_uid_2: true,
          currentTab: 1
        })

      }else if(option.id == 3) {
        that.setData({
          my_uid_3: true,
          currentTab: 2
        })

      }else if(option.id == 4) {
        that.setData({
          my_uid_4: true,
          currentTab: 3
        })

      }
    }

    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
//console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
  },
  footerTap: app.footerTap,
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  //index.php?act=other&op=share_bimg
  //背景图设置
  share_bimg() {
    let that = this;
    wx.request({
      url: ApiUrl + '/index.php?act=other&op=share_bimg',
      data: {
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (result) {
        console.log('背景图设置:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let res = result.data.datas;
            console.log('背景图设置:==>', res);
            that.setData({
              share_bimg: res
            })
          }
        }
      }
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    console.log(res);
    let voucher_id = res.target.dataset.voucher_id;
    let imgUrl = this.data.share_bimg;
    // let voucher_id = this.data.voucher_id;
    console.log('用户点击右上角分享==>',voucher_id)
    if (res.from === 'button') {
      // 来自页面内转发按钮
      if (res.target.id == 1) {
        this.setData({
          currentTab: 0
        });
        this.send_vouvoucher_list(voucher_id);
        console.log('res.target.id==>', res.target.id);
        return {
          title: '有好友赠送你一张优惠券，快来笑纳吧',
          imageUrl: '/image/WechatIMG91.jpeg',//图片地址
          path: '/pages/coupon_page/coupon_page?voucher_id=' + voucher_id + '&&id=' + res.target.id,// 用户点击首先进入的当前页面
          success: function (res) {
            // 转发成功
            console.log("转发成功:");
          },
          fail: function (res) {
            // 转发失败
            console.log("转发失败:");
          }
        }
      }
      if (res.target.id == 2) {
        this.setData({
          currentTab: 1
        });
        this.send_goods_vou(voucher_id);
        console.log('res.target.id==>', res.target.id);

        return {
          title: '有好友赠送你一张商品券，快来笑纳吧',
          imageUrl: imgUrl.bimg3,//图片地址
          path: '/pages/coupon_page/coupon_page?voucher_id=' + voucher_id + '&&id=' + res.target.id,// 用户点击首先进入的当前页面
          success: function (res) {
            // 转发成功
            console.log("转发成功:");
          },
          fail: function (res) {
            // 转发失败
            console.log("转发失败:");
          }
        }

      }
      if (res.target.id == 3) {
        this.send_gift(voucher_id);
        console.log('res.target.id==>', res.target.id);
        this.setData({
          currentTab: 2
        });
        return {
          title: '有好友赠送你一张商品礼券，快来笑纳吧',
          imageUrl: imgUrl.bimg4,//图片地址
          path: '/pages/coupon_page/coupon_page?voucher_id=' + voucher_id + '&&id=' + res.target.id,// 用户点击首先进入的当前页面
          success: function (res) {
            // 转发成功
            console.log("转发成功:");
          },
          fail: function (res) {
            // 转发失败
            console.log("转发失败:");
          }
        }


      }
      if (res.target.id == 4) {
        this.setData({
          currentTab: 3
        });
        console.log('res.target.id==>',res.target.id);
        return {
          title: '有好友赠送你一张临时会员卡，快来笑纳吧',
          imageUrl: imgUrl.bimg5,//图片地址
          path: '/pages/coupon_page/coupon_page?voucher_id=' + voucher_id + '&&id='+res.target.id,// 用户点击首先进入的当前页面
          success: function (res) {
            // 转发成功
            console.log("转发成功:");
          },
          fail: function (res) {
            // 转发失败
            console.log("转发失败:");
          }
        }


      }
    } 
  }
})
